package com.lemon.cloud.gray.support;

import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.experimental.UtilityClass;

/**
 * @author huangds
 * <p>
 * 灰度版本号传递工具 ,在非web 调用feign 传递之前手动setVersion
 */
@UtilityClass
public class GrayFlagContextHolder {

    private final ThreadLocal<String> THREAD_LOCAL_VERSION = new TransmittableThreadLocal<>();

    /**
     * TTL 设置灰度标识<br/>
     * @param grayFlag 灰度标识
     */
    public void setGrayFlag(String grayFlag) {
        THREAD_LOCAL_VERSION.set(grayFlag);
    }

    /**
     * 获取TTL中的灰度标识
     * @return 版本 || null
     */
    public String getGrayFlag() {
        return THREAD_LOCAL_VERSION.get();
    }

    public void clear() {
        THREAD_LOCAL_VERSION.remove();
    }

}
